package com.levi.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class PackageScanUtil {

	/**
	 * 包扫描
	 * 
	 * @param packags
	 *            com.sxt.service.impl /com/sxt/service/impl
	 * @return
	 */
	public static List<Class<?>> scan(String packags) {
		// 只有扫描，必须读取文件
		// 该项目的地址写死
		// 该成获取该项目的地址
		String projectDir = Thread.currentThread().getContextClassLoader().getResource("").getPath();
		String pkgRep = packags.replace(".", "\\");
		String path = projectDir + pkgRep;
		File dir = new File(path);
		List<Class<?>> classs = new ArrayList<Class<?>>();
		if (dir.isDirectory()) {
			File[] listFiles = dir.listFiles();
			for (File file : listFiles) {
				String dirProject = dir.getPath().replace(pkgRep, ""); // D:\workspace\Link\target\classes
				String javaFileName = file.getPath().replace(dirProject, "").replace("\\", ".");// D:\workspace\Link\target\classes\com\sxt\core\impl\RandomServerLoadBalance.class
				String className = javaFileName.substring(0, javaFileName.lastIndexOf("."));
				Class<?> clazz = null;
				try {
					clazz = Class.forName(className);
				} catch (Exception e) {
				}
				classs.add(clazz);
			}
		}
		return classs;
	}

	// public static void main(String[] args) {
	// List<Class<?>> scan = scan("com.sxt.core.impl");
	// System.out.println(scan);
	// }
}
